/*
编写 : Hexh
时间 : 2012-5-25 15:23:01
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using HMIS.FrameWork;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using HMIS.Infrastructure.Facade;
using HMIS.Infrastructure.Core.Entities;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of InfraApproveLog
	/// </summary>
	public class InfraApproveLogController : BaseController
	{

		private readonly IInfraApproveLogFacade InfraApproveLogFacade;
		private readonly IHospUserFacade HospUserFacade;
		
		public InfraApproveLogController(IInfraApproveLogFacade InfraApproveLogFacade, IHospUserFacade HospUserFacade)
		{
			this.InfraApproveLogFacade = InfraApproveLogFacade;
			this.HospUserFacade = HospUserFacade;
		}
		
		[Description("访问InfraApproveLog管理页面")]
		[ViewPage]
		public ActionResult InfraApproveLog()
		{
			return View();
		}
		
		[ExtResult]
		[Description("添加修改InfraApproveLog信息")]
		public ActionResult SaveOrUpdate()
		{
			InfraApproveLog entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new InfraApproveLog
				{
					ApproveItemId = Request["ApproveItemId"], 
					ApproveId = Request["ApproveId"], 
					ApproveStep = Request["ApproveStep"]==null ? 0 : int.Parse(Request["ApproveStep"]), 
					UserId = Request["UserId"], 
					Scale = Request["Scale"]==null ? 0 : int.Parse(Request["Scale"]), 
					AprCode = Request["AprCode"], 
					ApproveState = Request["ApproveState"], 
					ApproveUserId = Request["ApproveUserId"]					
				};
			}
			else
			{
				// get the entity.
				entity = InfraApproveLogFacade.Get(Request["Id"]);
				// set new value..
				entity.ApproveItemId = Request["ApproveItemId"]; 
				entity.ApproveId = Request["ApproveId"]; 
				entity.ApproveStep = Request["ApproveStep"]==null ? 0 : int.Parse(Request["ApproveStep"]); 
				entity.UserId = Request["UserId"]; 
				entity.Scale = Request["Scale"]==null ? 0 : int.Parse(Request["Scale"]); 
				entity.AprCode = Request["AprCode"]; 
				entity.ApproveState = Request["ApproveState"]; 
				entity.ApproveUserId = Request["ApproveUserId"];
				entity.ApproveDate = System.DateTime.Now;
			}
			// 保存信息
			InfraApproveLogFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}
		
		[ExtResult]
		[Description("删除InfraApproveLog基本资料")]
		public ActionResult Delete()
		{
			InfraApproveLogFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}
		
		[ExtResult]
		[Description("物理删除InfraApproveLog基本资料")]
		public ActionResult PhysicsDelete()
		{
			InfraApproveLogFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取InfraApproveLog清单")]
		public ActionResult GetPlist()
		{
			long total;
			var data = InfraApproveLogFacade.GetPlist(
				Request["approveItemId"],
				Request["approveType"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			//TODO : 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<InfraApproveLog> { total = total, data = data },
				new string[] { "User", "ApproveUser" });
		}

		[Description("获取InfraApproveLog信息")]
		public ActionResult GetById()
		{
			var entity = InfraApproveLogFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<InfraApproveLog> { data = new InfraApproveLog[] { entity } }, new string[] { "User", "ApproveUser" }
			   );
		}

		[ExtResult]
		[Description("判断InfraApproveLog字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = InfraApproveLogFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
